import { Controller } from 'egg';
import { CommonUtil } from '../../utils';
import { createRandomString } from '../../utils/common';

export default class HomeController extends Controller {
  public async index() {
    const { ctx } = this;
    if (ctx.request.url.startsWith('/api')) {
      ctx.response.status = 404;
    } else {
      if (CommonUtil.isPC(ctx.request.headers['user-agent'])) {
        ctx.body = await ctx.renderView('index.html');
      } else {
        ctx.body = await ctx.renderView('mobile.ejs');
      }
    }
  }
  public async test() {
    const { ctx } = this;
    // ctx.body = await ctx.model.User.create({ username: 'zjz', password: 123 });
    // const token = ctx.app.jwt.sign({ foo: 'bar' }, ctx.app.config.jwt.secret, {
    //   expiresIn: '60m',
    // });
    // const token = await ctx.app.redis.get('token');
    const token = createRandomString();
    ctx.body = token;
  }
}
